import axios from '../../utils/axios'

Component({

  options: {
    styleIsolation: 'apply-shared',
    multipleSlots: true
  },

  /**
   * 组件的属性列表
   */
  properties: {
    flag: Boolean, // 展示布局切换
    commodityList: Object, // 商品list
    showCount: {
      type: Number,
      value: 2
    } // 一行要展示的个数，默认为2
  },

  /**
   * 组件的初始数据
   */
  data: {
    shopp: false,
    commodityInfo: null,
    classifyIndex: 0, // 规格index
  },

  /**
   * 组件的方法列表
   */
  methods: {

    // 点击购买
    shoppBuy: function (e) {
      let goodsId = e.currentTarget.dataset.goodsid

      for (let i = 0; i < this.properties.commodityList.length; i++) {
        if (this.properties.commodityList[i].goods_id == goodsId) {
          // 无规格，则给一个通用
          if (this.properties.commodityList[i].attach == undefined || this.properties.commodityList[i].attach.length <= 0) {
            let param = [{
              attach_id: '-1',
              name: '通用',
              discount: 0,
              price: this.properties.commodityList[i].showMoney,
              stock: this.properties.commodityList[i].stock
            }]
            this.properties.commodityList[i]['attach'] = param
            this.setData({
              classifyIndex: 0,
              commodityInfo: this.properties.commodityList[i],
              shopp: true
            })
          } else {
            this.setData({
              classifyIndex: 0,
              commodityInfo: this.properties.commodityList[i],
              shopp: true
            })
          }
        }
      }

    },

    // 跳转商品详情
    checkDetails: function (e) {
      let goodsId = e.currentTarget.dataset.goodsid
      wx.navigateTo({
        url: '/pages/details/details?goodsId=' + goodsId
      })
    },

  }

})